#include <iostream>

#include "CService.h"
#include "CServiceRepository.h"

#include "space/CSpace.h"
#include "space/CMessage.h"
#include "space/CKeyValue.h"

// user service
class CEchoService : public CService
{
public :
	CEchoService() : CService() {};
	CEchoService(char *name) : CService(name) {};
	CEchoService(const CEchoService &rhs) : CService(rhs) {};
	~CEchoService() {};

	CEchoService *clone() const { return new CEchoService(*this); };

	void run(cube__CObject *in, struct cube__invokeResponse &res) {
		res.out = in;
	};
};


int main(int argc, char **argv)
{
	CServiceRepository *service_repository = NULL;

	// cgi
	if(argc<2) {
		service_repository = new CServiceRepository();
	}
	// stand-alone
	else {
		service_repository = new CServiceRepository(atoi(argv[1]));
	}

	CEchoService service1("sample.echo.service1");
	CEchoService service2("sample.echo.service2");

	service_repository->plugin(&service1);
	service_repository->plugin(&service2);

	// cgi
	if(argc<2) service_repository->runCGI();
	// stand-alone
	else {
		char *name = NULL;;
		cout << "# registered services" << endl;
		cout << service1.getName() << endl;
		cout << service2.getName() << endl;

		service_repository->run();
	}
	
	delete service_repository;

        return 1;
}
